<template>
  <el-dialog :title="title" :visible="showAdd" width="50%" @close="handleClose">
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <quillEditor
          v-model="form.articleBody"
          @blur="handleBlur"
          ref="QuillEditor"
          :options="editorOption"
          :value="form.articleBody"
        ></quillEditor>
      </el-form-item>
      <el-form-item label="视频地址" prop="videoURL">
        <el-input v-model="form.videoURL"></el-input>
      </el-form-item>
    </el-form>
    <template #footer class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// import RichText from '../components/richText.vue'
import { quillEditor } from 'vue-quill-editor'
import { add, detail, update } from '@/api/hmmm/articles'
export default {
  props: {
    showAdd: {
      type: Boolean,
      default: false
    }
  },
  components: {
    quillEditor
  },
  data () {
    const toolbarOptions = [
      ['bold', 'italic', 'underline', 'strike'],
      [{ list: 'ordered' }, { list: 'bullet' }],
      ['blockquote'],
      ['code-block'],
      ['link', 'image', 'video']
    ]
    return {
      // showAddFn: this.showAdd,
      editorOption: {
        placeholder: '请输入编辑内容',
        theme: 'snow', // 主题风格
        modules: {
          toolbar: {
            container: toolbarOptions // 工具栏
          }
        }
      },
      // 富文本编辑器配置
      form: { // 表单数据
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '文章内容不能为空', trigger: 'blur' }
        ]
      },
      title: '添加文章'
    }
  },
  methods: {
    // 关闭对话框
    handleClose () {
      this.$emit('update:showAdd', false)
      this.$refs.form.resetFields()
      this.form = { // 表单数据
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    // 富文本框的失焦校验
    handleBlur () {
      console.log(11)
      this.$refs.form.validateField('articleBody')
      console.log(111)
    },
    handleSubmit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.form.id) { // 编辑
            await update(this.form)
            this.$message.success('修改文章成功')
          } else { // 新增
            await add(this.form)
            this.$message.success('新增文章成功')
          }
          this.handleClose()
          this.$emit('reload')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 根据id获取文章详情
    async getDetailArticle (row) {
      // console.log(11)
      const { data } = await detail(row)
      this.form = data
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .ql-editor{
  height: 200px;
}
</style>
